স্প্রিং বুট ক্লায়েন্ট RestTemplate
বা WebClient
ব্যবহার করে বিভিন্ন HTTP মেথড (GET, POST, PUT, DELETE, ইত্যাদি) এর মাধ্যমে API এর সাথে যোগাযোগ করতে পারে।
এখানে প্রতিটি HTTP মেথডের ব্যবহার এবং হ্যান্ডলিং নিয়ে আলোচনা করা হয়েছে:
GET
রিকোয়েস্ট সাধারণত ডাটা রিট্রাইভ করতে ব্যবহৃত হয়।
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String getData(String url) {
return restTemplate.getForObject(url, String.class);
}
}
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://example.com").build();
}
public String getData(String endpoint) {
return webClient.get()
.uri(endpoint)
.retrieve()
.bodyToMono(String.class)
.block(); // Reactive এর জন্য block() করা হচ্ছে
}
}
POST
রিকোয়েস্ট সাধারণত ডাটা সার্ভারে সাবমিট করতে ব্যবহৃত হয়।
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> postData(String url, Object requestBody) {
return restTemplate.postForEntity(url, requestBody, String.class);
}
}
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://example.com").build();
}
public String postData(String endpoint, Object requestBody) {
return webClient.post()
.uri(endpoint)
.bodyValue(requestBody)
.retrieve()
.bodyToMono(String.class)
.block();
}
}
PUT
রিকোয়েস্ট সাধারণত বিদ্যমান ডাটা আপডেট করতে ব্যবহৃত হয়।
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void updateData(String url, Object requestBody) {
restTemplate.put(url, requestBody);
}
}
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://example.com").build();
}
public String updateData(String endpoint, Object requestBody) {
return webClient.put()
.uri(endpoint)
.bodyValue(requestBody)
.retrieve()
.bodyToMono(String.class)
.block();
}
}
DELETE
রিকোয়েস্ট সাধারণত সার্ভার থেকে ডাটা মুছে ফেলতে ব্যবহৃত হয়।
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void deleteData(String url) {
restTemplate.delete(url);
}
}
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://example.com").build();
}
public void deleteData(String endpoint) {
webClient.delete()
.uri(endpoint)
.retrieve()
.bodyToMono(Void.class)
.block();
}
}
exchange
মেথড ব্যবহার করে কাস্টম HTTP মেথড হ্যান্ডল করতে পারেন।
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> exchangeRequest(String url, HttpMethod method, Object body) {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer your-token");
HttpEntity<Object> entity = new HttpEntity<>(body, headers);
return restTemplate.exchange(url, method, entity, String.class);
}
}
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://example.com").build();
}
public String exchangeRequest(String endpoint, HttpMethod method, Object body) {
return webClient.method(method)
.uri(endpoint)
.bodyValue(body)
.retrieve()
.bodyToMono(String.class)
.block();
}
}
RestTemplate
এর ResponseErrorHandler
বা WebClient
এর onStatus()
ব্যবহার করুন।HttpHeaders
ব্যবহার করে রিকোয়েস্টে কাস্টম হেডার যুক্ত করুন।WebClient
ব্যবহার করুন।এভাবে, আপনি স্প্রিং বুট ক্লায়েন্টে REST API এর বিভিন্ন HTTP মেথড দক্ষতার সাথে হ্যান্ডল করতে পারবেন।
Spring Boot-এ RestTemplate
এবং WebClient
ব্যবহার করে GET, POST, PUT, DELETE, এবং PATCH রিকোয়েস্ট পরিচালনা করা যায়। এখানে প্রতিটি রিকোয়েস্টের উদাহরণ দেওয়া হলো:
GET রিকোয়েস্ট সাধারণত ডেটা রিট্রিভ করার জন্য ব্যবহৃত হয়।
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
@Autowired
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String fetchData() {
String url = "https://api.example.com/data";
return restTemplate.getForObject(url, String.class);
}
}
POST রিকোয়েস্ট নতুন ডেটা তৈরি করার জন্য ব্যবহৃত হয়।
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String createData(Object requestData) {
String url = "https://api.example.com/data";
return restTemplate.postForObject(url, requestData, String.class);
}
}
PUT রিকোয়েস্ট বিদ্যমান ডেটা আপডেট করার জন্য ব্যবহৃত হয়।
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void updateData(Long id, Object updatedData) {
String url = "https://api.example.com/data/" + id;
restTemplate.put(url, updatedData);
}
}
DELETE রিকোয়েস্ট বিদ্যমান ডেটা মুছে ফেলার জন্য ব্যবহৃত হয়।
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void deleteData(Long id) {
String url = "https://api.example.com/data/" + id;
restTemplate.delete(url);
}
}
PATCH রিকোয়েস্ট আংশিক আপডেট করার জন্য ব্যবহৃত হয়।
RestTemplate
সরাসরি PATCH রিকোয়েস্ট সমর্থন করে না, তবে exchange()
ব্যবহার করে এটি করা সম্ভব।
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String patchData(Long id, Object partialUpdate) {
String url = "https://api.example.com/data/" + id;
HttpEntity<Object> requestEntity = new HttpEntity<>(partialUpdate);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PATCH, requestEntity, String.class);
return response.getBody();
}
}
public Mono<String> fetchData() {
return webClient.get()
.uri("/data")
.retrieve()
.bodyToMono(String.class);
}
public Mono<String> createData(Object requestData) {
return webClient.post()
.uri("/data")
.bodyValue(requestData)
.retrieve()
.bodyToMono(String.class);
}
public Mono<Void> updateData(Long id, Object updatedData) {
return webClient.put()
.uri("/data/{id}", id)
.bodyValue(updatedData)
.retrieve()
.bodyToMono(Void.class);
}
public Mono<Void> deleteData(Long id) {
return webClient.delete()
.uri("/data/{id}", id)
.retrieve()
.bodyToMono(Void.class);
}
public Mono<String> patchData(Long id, Object partialUpdate) {
return webClient.patch()
.uri("/data/{id}", id)
.bodyValue(partialUpdate)
.retrieve()
.bodyToMono(String.class);
}
আপনার অ্যাপ্লিকেশনের প্রয়োজন অনুসারে GET, POST, PUT, DELETE এবং PATCH রিকোয়েস্টগুলো সহজেই পরিচালনা করা যায়।
Spring Boot-এ ResponseEntity একটি গুরুত্বপূর্ণ ক্লাস যা HTTP রেসপন্সের স্ট্যাটাস কোড, হেডার, এবং বডি পরিচালনা করতে ব্যবহৃত হয়। HTTP Methods এর মাধ্যমে ResponseEntity হ্যান্ডল করার পদ্ধতি নিম্নে আলোচনা করা হলো:
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> getDataFromApi() {
String url = "https://jsonplaceholder.typicode.com/posts";
// API থেকে ResponseEntity গ্রহণ করা
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
return response;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@Autowired
private ApiService apiService;
@GetMapping("/getPosts")
public ResponseEntity<String> getPosts() {
return apiService.getDataFromApi();
}
}
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> postDataToApi(String jsonPayload) {
String url = "https://jsonplaceholder.typicode.com/posts";
HttpEntity<String> request = new HttpEntity<>(jsonPayload);
// POST Method-এর মাধ্যমে ডেটা পাঠানো
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
return response;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@Autowired
private ApiService apiService;
@PostMapping("/createPost")
public ResponseEntity<String> createPost(@RequestBody String jsonPayload) {
return apiService.postDataToApi(jsonPayload);
}
}
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> updateDataInApi(String jsonPayload, int id) {
String url = "https://jsonplaceholder.typicode.com/posts/" + id;
HttpEntity<String> request = new HttpEntity<>(jsonPayload);
// PUT Method ব্যবহার
restTemplate.put(url, request);
return new ResponseEntity<>("Resource updated successfully", HttpStatus.OK);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@Autowired
private ApiService apiService;
@PutMapping("/updatePost")
public ResponseEntity<String> updatePost(@RequestBody String jsonPayload, @RequestParam int id) {
return apiService.updateDataInApi(jsonPayload, id);
}
}
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> deleteDataFromApi(int id) {
String url = "https://jsonplaceholder.typicode.com/posts/" + id;
// DELETE Method ব্যবহার
restTemplate.delete(url);
return new ResponseEntity<>("Resource deleted successfully", HttpStatus.OK);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@Autowired
private ApiService apiService;
@DeleteMapping("/deletePost")
public ResponseEntity<String> deletePost(@RequestParam int id) {
return apiService.deleteDataFromApi(id);
}
}
Custom HTTP Status Code:
return new ResponseEntity<>("Error occurred", HttpStatus.BAD_REQUEST);
Custom HTTP Headers:
HttpHeaders headers = new HttpHeaders();
headers.set("Custom-Header", "HeaderValue");
return new ResponseEntity<>("Response Body", headers, HttpStatus.OK);
ResponseEntity.ok() Shortcut:
return ResponseEntity.ok("Response Body");
ResponseEntity.noContent() for DELETE:
return ResponseEntity.noContent().build();
Spring Boot-এ ResponseEntity ব্যবহার করে HTTP Methods এর জন্য রেসপন্স বডি, হেডার, এবং স্ট্যাটাস কোড সহজেই হ্যান্ডল করা যায়। এটি অ্যাপ্লিকেশনকে আরো লচিৎ এবং কাস্টমাইজড রেসপন্স প্রদান করতে সহায়তা করে।
যদি আরো বিস্তারিত সাহায্য প্রয়োজন হয়, জানান! 😊
স্প্রিং বুটে RestTemplate
বা WebClient
ব্যবহার করে বিভিন্ন HTTP মেথড যেমন GET
, POST
, PUT
, DELETE
, ইত্যাদির মাধ্যমে RESTful API-তে অনুরোধ করা যায়। নিচে উদাহরণসহ এই মেথডগুলোর ব্যবহার দেখানো হলো:
GET
মেথড সাধারণত সার্ভার থেকে ডেটা সংগ্রহের জন্য ব্যবহৃত হয়।
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class RestTemplateClient {
private final RestTemplate restTemplate;
public RestTemplateClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String getExample(String url) {
return restTemplate.getForObject(url, String.class);
}
}
String response = restTemplateClient.getExample("https://jsonplaceholder.typicode.com/posts/1");
System.out.println(response);
POST
মেথড ব্যবহার করে নতুন ডেটা তৈরি করা হয় বা সার্ভারে তথ্য পাঠানো হয়।
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class PostClient {
private final RestTemplate restTemplate;
public PostClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String createPost(String url, Object requestBody) {
ResponseEntity<String> response = restTemplate.postForEntity(url, requestBody, String.class);
return response.getBody();
}
}
Map<String, String> requestBody = new HashMap<>();
requestBody.put("title", "foo");
requestBody.put("body", "bar");
requestBody.put("userId", "1");
String response = postClient.createPost("https://jsonplaceholder.typicode.com/posts", requestBody);
System.out.println(response);
PUT
মেথড ব্যবহার করে বিদ্যমান ডেটা আপডেট করা হয়।
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class PutClient {
private final RestTemplate restTemplate;
public PutClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void updatePost(String url, Object requestBody) {
restTemplate.put(url, requestBody);
}
}
Map<String, String> requestBody = new HashMap<>();
requestBody.put("id", "1");
requestBody.put("title", "updated title");
requestBody.put("body", "updated body");
requestBody.put("userId", "1");
putClient.updatePost("https://jsonplaceholder.typicode.com/posts/1", requestBody);
DELETE
মেথড ব্যবহার করে ডেটা মুছে ফেলা হয়।
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class DeleteClient {
private final RestTemplate restTemplate;
public DeleteClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void deletePost(String url) {
restTemplate.delete(url);
}
}
deleteClient.deletePost("https://jsonplaceholder.typicode.com/posts/1");
WebClient
রিয়াক্টিভ প্রোগ্রামিংয়ের জন্য ব্যবহৃত হয়। এটি RestTemplate-এর বিকল্প।
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class WebClientExample {
private final WebClient webClient;
public WebClientExample(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://jsonplaceholder.typicode.com").build();
}
// GET Method
public String getPost(int id) {
return webClient.get()
.uri("/posts/{id}", id)
.retrieve()
.bodyToMono(String.class)
.block();
}
// POST Method
public String createPost(Object requestBody) {
return webClient.post()
.uri("/posts")
.bodyValue(requestBody)
.retrieve()
.bodyToMono(String.class)
.block();
}
// PUT Method
public void updatePost(int id, Object requestBody) {
webClient.put()
.uri("/posts/{id}", id)
.bodyValue(requestBody)
.retrieve()
.toBodilessEntity()
.block();
}
// DELETE Method
public void deletePost(int id) {
webClient.delete()
.uri("/posts/{id}", id)
.retrieve()
.toBodilessEntity()
.block();
}
}
// GET
String getResponse = webClientExample.getPost(1);
System.out.println(getResponse);
// POST
Map<String, String> requestBody = new HashMap<>();
requestBody.put("title", "foo");
requestBody.put("body", "bar");
requestBody.put("userId", "1");
String postResponse = webClientExample.createPost(requestBody);
System.out.println(postResponse);
// PUT
requestBody.put("title", "updated title");
webClientExample.updatePost(1, requestBody);
// DELETE
webClientExample.deletePost(1);
getForObject()
, getForEntity()
postForObject()
, postForEntity()
put()
delete()
get()
post()
put()
delete()
উপরের উদাহরণগুলো অনুসরণ করে আপনি স্প্রিং বুট ক্লায়েন্ট দিয়ে যেকোন HTTP মেথড ব্যবহার করতে পারবেন।
Read more